package com.myk.game.lib.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatTextView;
import com.blankj.utilcode.util.StringUtils;
import com.myk.game.lib.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class TimeTextView extends AppCompatTextView {

    private final TimeTextView textView;
    private String date;
    private String format;
    private final TimeHandler mTimeHandler = new TimeHandler();

    public TimeTextView(Context context) {
        this(context, null);
    }

    public TimeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.textView = this;

        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.TimeTextView);
        format = mTypedArray.getString(R.styleable.TimeTextView_ttv_format);
        if (StringUtils.isEmpty(format)){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        mTypedArray.recycle();
    }

    /**
     * 返回当前的日期文本
     */
    public String getDate() {
        return date;
    }

    /**
     * 设置日期格式
     */
    public void setFormat(String format){
        this.format = format;
        updateClock();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        init();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mTimeHandler.stopScheduleUpdate();
    }

    private void init() {
        try {
            //初始化textview显示时间
            updateClock();
            //更新线程开始
            new Thread(new Runnable() {
                @Override
                public void run() {
                    mTimeHandler.startScheduleUpdate();
                }
            }).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //更新当前时日期
    private void updateClock() {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        date = sdf.format(new Date());
        textView.setText(date);
    }

    //更新Handler通过handler的延时发送消息来更新日期
    @SuppressLint("HandlerLeak")
    private class TimeHandler extends Handler {
        private boolean mStopped;

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (!mStopped) {
                updateClock();
                post();//实现实时更新
            }
        }

        //每隔1秒发送一次消息
        private void post() {
            sendMessageDelayed(obtainMessage(0), 1000);
        }

        //开始更新
        public void startScheduleUpdate() {
            mStopped = false;
            post();
        }

        //停止更新
        public void stopScheduleUpdate() {
            mStopped = true;
            removeMessages(0);
        }
    }


}
